package com.hdu.point_offer;

import java.util.HashMap;
import java.util.Map;

public class lcr154 {

    class Node {
        int val;
        Node next;
        Node random;

        public Node(int val) {
            this.val = val;
            this.next = null;
            this.random = null;
        }
    }

    class Solution {
        public Node copyRandomList(Node head) {
            Map<Node, Node> nodeRels = new HashMap<>();
            Node cur = head;
            while (cur != null) {
                nodeRels.put(cur, new Node(cur.val));
                cur = cur.next;
            }
            cur = head;
            while (cur != null)
            {
                if (cur.next != null)
                    nodeRels.get(cur).next = nodeRels.get(cur.next);
                if (cur.random != null)
                    nodeRels.get(cur).random = nodeRels.get(cur.random);
                cur = cur.next;
            }
            return nodeRels.get(head);
        }
    }
}
